﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Web7.Core.BootStrapperTask;
using Web7.Core.InversionOfControl;
using Web7.FilterProviders;
using Web7.Core.Configuration;
using Web7.Core.Apps;
using Web7.Extensions;
using Web7.Ioc;
///using Web7.Ioc;

namespace Web7.Core.BootStrapperTasks
{
    /// <summary>
    /// 引导程序，用于加载模块
    /// </summary>
    public class LoadApps : IBootStrapperTask
    {
        //private readonly IDependencyInjector _dependencyInjector;
         private readonly IResolutionContext _resolutionContext;
        //public LoadApps(IDependencyInjector dependencyInjector)
         public LoadApps(IResolutionContext resolutionContext)
        {
            this._resolutionContext = resolutionContext;
        }

        #region IBootStrapperTask Members

        /// <summary>
        /// 执行引导程序
        /// </summary>
        /// <param name="bootStrapperState">引导程序状态</param>
        public void Execute(IDictionary<string, object> bootStrapperState)
        {
            //已经存在的单例
            IAppRegistry appRegistry = this._resolutionContext.Resolve<IAppRegistry>();
            RouteCollection routes = this._resolutionContext.Resolve<RouteCollection>();
            ModelBinderDictionary modelBinders = this._resolutionContext.Resolve<ModelBinderDictionary>();
            Web7ConfigurationSection appConfig = this._resolutionContext.Resolve<Web7ConfigurationSection>();

            //ActionFilter注册表
            GlobalFilterCollection globalFilters = this._resolutionContext.Resolve<GlobalFilterCollection>();
            FilterRegistryFilterProvider filterRegistry = this._resolutionContext.Resolve<FilterRegistryFilterProvider>();
            globalFilters.Clear();
            filterRegistry.Clear();
            routes.Clear();

            //遍历配置文件中的modules节点，获取根据module配置信息创建实例并执行相关方法
            foreach (Web7AppConfigurationElement app in appConfig.Apps)
            {
                IApp appInstance = appRegistry.Load(appConfig, app);

                if (appInstance != null)
                {
                    appInstance.Initialize();
                    appInstance.RegisterModelBinders(modelBinders);
                }
            }

            // 设置路由表
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            appRegistry.RegisterRoutes();

            //由于IWeb7Module.RegisterFilters方法内部可能会用到注册的路由信息(如RouteFilterCriteria)，所以该方法路由注册(RegisterRoutes)之后调用
            foreach (IApp app in appRegistry.GetApps())
                app.RegisterFilters(filterRegistry, globalFilters);
        }

        /// <summary>
        /// 引导程序清理
        /// </summary>
        /// <param name="bootStrapperState">引导程序状态</param>
        public void Cleanup(IDictionary<string, object> bootStrapperState)
        {
            _resolutionContext.Resolve<IAppRegistry>().UnloadApps();
        }

        #endregion
    }
}
